/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.burlapclient.factory;

import com.caucho.burlap.client.BurlapProxyFactory;
import com.hyts.bind.domain.BindDomain;
import com.hyts.bind.domain.BindDomains;
import com.hyts.bind.facade.BindContext;
import com.hyts.burlapclient.annotation.BurlapReferenceBean;
import com.hyts.toolbox.caller.Container;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.burlap.initializer.service
 * @author:LiBo/Alex
 * @create-date:2020-04-06 22:04
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
@Slf4j
public class BurlapServiceFactory {

    static Map<String,Object> BurlapServiceFactory = new HashMap<>();

    /**
     * 啟動服務操作
     * @param scanPackage
     */
    public static void startup(String scanPackage){
        List<Class<?>> classList = Container.SCANNER.get().includePackage(scanPackage).
                annotation(BurlapReferenceBean.class).scanToClass();
        BurlapProxyFactory factory=new BurlapProxyFactory();
        classList.parallelStream().forEach(param->{
            BurlapReferenceBean burlapReferenceBean = param.getAnnotation(BurlapReferenceBean.class);
            Object paramValue = null;
            try {
                paramValue = factory.create(param,burlapReferenceBean.value());
            } catch (MalformedURLException e) {
                log.error("创建burlap消费对象失败!",e);
            }
            BurlapServiceFactory.put(param.getName(),paramValue);
            log.info("创建及绑定{}-{}",param,paramValue);
        });
    }

    public static Map<String, Object> getBurlapServiceFactory() {
        return BurlapServiceFactory;
    }
}
